<template>
  <div class="user-center">
    <div class="user-box">
      <div class="user-left">
        <UserLeft></UserLeft>
      </div>
      <div class="user-right">
        <div class="forumtitle-box">
          <div class="forumtitle">修改密码</div>
        </div>
        <div class="input-box">
          <el-form ref="ruleForm" :rules="rules" label-position="right" label-width="142px" :model="ruleForm">
            <el-form-item label="旧密码：" prop="oldPassword">
              <el-input v-model="ruleForm.oldPassword"></el-input>
            </el-form-item>
            <el-form-item label="新密码：" prop="newPassword">
              <el-input v-model="ruleForm.newPassword"></el-input>
            </el-form-item>
            <el-form-item label="确认密码：" prop="confirmPassword">
              <el-input v-model="ruleForm.confirmPassword"> </el-input>
            </el-form-item>
          </el-form>
          <div>
            <el-button class="preservation" @click="submit()" type="primary">保存</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import UserLeft from "./components/UserLeft";
  import {
    mapGetters,
    mapActions,
    mapMutations
  } from "vuex";
  export default {
    components: {
      UserLeft,
    },
    data() {
      const validatePassword = (rule, password, callback) => {
        // 至少8位
        const minLength = 8;
        // 至少一个字母
        const hasLetter = /[a-zA-Z]/;
        // 至少一个数字
        const hasDigit = /[0-9]/;
        // 至少一个特殊字符
        const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>]/;

        // 检查密码长度
        if (password.length < minLength ||
          !hasLetter.test(password) ||
          !hasDigit.test(password) ||
          !hasSpecialChar.test(password)) {
          callback(new Error("密码要求是8位以上，字母、数字、特殊字符组合"));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          oldPassword: "",
          newPassword: "",
          confirmPassword: "",
        },
        rules: {
          oldPassword: [{
              required: true,
              message: "请输入旧密码",
              trigger: "blur"
            },
            // {
            //   min: 8,
            //   message: "密码长度8位以上",
            //   trigger: "blur",
            // },

          ],
          newPassword: [{
              required: true,
              message: "请输入新密码",
              trigger: "blur"
            },
            {
              min: 6,
              max: 20,
              message: "密码长度8位以上",
              trigger: "blur",
            },
            {
              required: true,
              trigger: ["blur", "change"],
              validator: validatePassword,
            },
          ],
          confirmPassword: [{
              required: true,
              message: "请确认新密码",
              trigger: "blur"
            },
            {
              min: 6,
              max: 20,
              message: "长度在 6 到 20 个字符",
              trigger: "blur",
            },
            {
              required: true,
              trigger: ["blur", "change"],
              validator: validatePassword,
            },
          ],
        },
      };
    },
    mounted() {},
    methods: {
      ...mapActions(["changePassword"]),
      submit() {
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            if (this.ruleForm.newPassword != this.ruleForm.confirmPassword) {
              this.$message.error("两次新密码不一致");
            } else {
              let data = {
                old_password: this.ruleForm.oldPassword,
                new_password: this.ruleForm.newPassword,
                new_passwords: this.ruleForm.confirmPassword,
              };
              this.changePassword(data).then((res) => {
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
                localStorage.setItem('password_status')
                this.$refs.ruleForm.resetFields();
              });
            }
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
    },
    computed: {},
  };
</script>

<style scoped>
  .user {
    padding: 1px 0 30px;
    background: #f7f7f7;
  }

  .user-box {
    width: 1200px;
    margin: 30px auto 0;
    display: flex;
  }

  .user-left {
    width: 300px;
    margin-right: 10px;
  }

  .user-right {
    flex: 1;
    background: #fff;
    min-height: 500px;
  }

  .forumtitle-box {
    height: 79px;
    width: 920px;
    margin-left: 20px;
    border-bottom: 1px solid #e8e8e8;
    margin-bottom: 40px;
  }

  .forumtitle {
    width: 80px;
    line-height: 76px;
    margin-left: 27px;
    font-size: 20px;
    font-family: Source Han Sans CN;
    font-weight: 500;
    color: #000000;
    border-bottom: 4px solid #0e93e4;
  }

  .input-box /deep/ .el-input__inner {
    width: 300px;
    height: 60px;
    font-size: 16px;
  }

  .input-box /deep/ .el-form-item {
    margin-bottom: 30px;
  }

  .input-box /deep/ .el-form-item__label {
    line-height: 60px;
    font-size: 16px;
    font-family: Source Han Sans CN;
    font-weight: 500;
    color: #000000;
  }

  .preservation {
    width: 200px;
    height: 60px;
    background: #0e93e4;
    border-radius: 3px;
    font-size: 18px;
    font-family: Source Han Sans CN;
    font-weight: 500;
    color: #ffffff;
    cursor: pointer;
    letter-spacing: 3px;
    margin-left: 242px;
    margin-top: 30px;
  }
</style>